from airtest.core.api import *

from base.base_other_page import BaseObject, BaseOperate


class GMPage(BaseObject):
    def __init__(self):
        super().__init__()
        # 退出按钮
        self.close_gm_btn = "UnitHeadCircleBaseRenderer"
        # GM命令页签
        self.gm_command_btn = "Content", "GMMenuToggle(Clone)", 0, "Icon"
        # 数据修改页签
        self.gm_data_modification_btn = "Content", "GMMenuToggle(Clone)", 2, "Icon"
        # cmd输入框
        self.cmd_input1 = "Command1", "InputField"
        # cmd确定执行按钮
        self.cmd_input1_determine_btn = "Command1", "Confirm"
        # 数据修改里全部资源确定按钮
        self.data_modification_all_resources_confirm = "Line1", "Confirm"
        # 快捷键-沙盘超神
        self.sand_table_supernatural = "Line1", "GMButton(Clone)", 2, "Text"
        # 快捷键页签
        self.shortcut_key = "Content", "GMMenuToggle(Clone)", 1, "Icon"

    # 快捷键-沙盘超神
    def find_sand_table_supernatural(self):
        return self.get_gm_element(self.sand_table_supernatural)

    # 快捷键页签
    def find_shortcut_key(self):
        return self.get_gm_element(self.shortcut_key)

    def find_close_gm_btn(self):
        return self.get_element_by_parent(self.close_gm_btn)

    def find_gm_command_btn(self):
        return self.get_gm_element(self.gm_command_btn)

    def find_cmd_input1(self):
        return self.get_element_parent_offspring(self.cmd_input1)

    def find_cmd_input1_determine_btn(self):
        return self.get_element_parent_offspring(self.cmd_input1_determine_btn)

    def find_gm_data_modification_btn(self):
        return self.get_gm_element(self.gm_data_modification_btn)

    def find_data_modification_all_resources_confirm(self):
        return self.get_element_parent_offspring(self.data_modification_all_resources_confirm)


class GMOperate(BaseOperate):
    def __init__(self):
        self.gmp = GMPage()

    # 快捷键-沙盘超神
    def click_sand_table_supernatural(self):
        self.gmp.find_sand_table_supernatural().click()

    # 快捷键页签
    def click_shortcut_key(self):
        self.gmp.find_shortcut_key().click()

    # 退出gm按钮
    def click_close_gm_btn(self):
        self.gmp.find_close_gm_btn().click()

    # 点击页签GM命令
    def click_gm_command_btn(self):
        self.gmp.find_gm_command_btn().click()

    # 输入
    def input1_gm_command(self, command):
        self.gmp.find_cmd_input1().set_text(command)

    # 点击输入框
    def click_input1_gm_command(self):
        self.gmp.find_cmd_input1().click()

    # 点击输入框确定按钮（需要点两次，一次从键盘输入法切回游戏，一次是点击确定）
    def click_cmd_input1_determine_btn(self):
        self.gmp.find_cmd_input1_determine_btn().click()
        self.gmp.find_cmd_input1_determine_btn().click()

    def click_gm_data_modification_btn(self):
        self.gmp.find_gm_data_modification_btn().click()

    def click_all_resources_confirm(self):
        self.gmp.find_data_modification_all_resources_confirm().click()


class GMBusiness:
    def __init__(self):
        self.gmo = GMOperate()

    def excute_gm_command(self, command):
        sleep(2)
        # GM命令页签
        self.gmo.click_gm_command_btn()
        # 点击输入框
        self.gmo.click_input1_gm_command()
        # 输入命令
        self.gmo.input1_gm_command(command)
        # 执行确定
        self.gmo.click_cmd_input1_determine_btn()
        time.sleep(1)
        # 数据修改(获取全部资源)
        self.gmo.click_gm_data_modification_btn()
        # 所有资源添加
        self.gmo.click_all_resources_confirm()
        time.sleep(1)
        # 点击快捷键
        self.gmo.click_shortcut_key()
        # 点击沙盘超神
        self.gmo.click_sand_table_supernatural()
        time.sleep(1)
        # 退出
        self.gmo.click_close_gm_btn()
